package com.hcms.common.api;

import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

import java.io.Serializable;
import java.util.Date;

@Data
@NoArgsConstructor
public class CommonResponse<T> implements Serializable {

    /**
     * 状态码
     */
    private Integer status;

    /**
     * 提示信息
     */
    private String message;

    /**
     * 数据封装
     */
    private T result;

    /**
     * 时间戳
     */
    private Long timestamp;

    public CommonResponse(HttpStatus status, String message) {
        this.status = status.value();
        this.message = message;
        this.result = null;
        this.timestamp = new Date().getTime();
    }

    public CommonResponse(HttpStatus status, String message, T result) {
        this.status = status.value();
        this.message = message;
        this.result = result;
        this.timestamp = new Date().getTime();
    }

    /**
     * 成功：200
     *
     * @param result
     * @param <T>
     * @return
     */
    public static <T> ResponseEntity<CommonResponse<T>> ok(T result) {
        CommonResponse<T> commonResponse = new CommonResponse<>(HttpStatus.OK, HttpStatus.OK.getReasonPhrase(), result);
        return ResponseEntity.status(HttpStatus.OK).body(commonResponse);
    }

    /**
     * 创建成功：201
     *
     * @param <T>
     * @return
     */
    public static <T> ResponseEntity<CommonResponse<T>> created(T result) {
        CommonResponse<T> commonResponse = new CommonResponse<>(HttpStatus.CREATED, HttpStatus.CREATED.getReasonPhrase(), result);
        return ResponseEntity.status(HttpStatus.CREATED).body(commonResponse);
    }

    /**
     * 接受：202
     *
     * @param <T>
     * @return
     */
    public static <T> ResponseEntity<CommonResponse<T>> accepted(String message) {
        CommonResponse<T> commonResponse = new CommonResponse<>(HttpStatus.ACCEPTED, message);
        return ResponseEntity.status(HttpStatus.ACCEPTED).body(commonResponse);
    }

    /**
     * 无内容：204
     *
     * @param message
     * @param <T>
     * @return
     */
    public static <T> ResponseEntity<CommonResponse<T>> noContent(String message) {
        CommonResponse<T> commonResponse = new CommonResponse<>(HttpStatus.NO_CONTENT, message);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).body(commonResponse);
    }

    /**
     * 错误请求：400
     *
     * @param message
     * @param <T>
     * @return
     */
    public static <T> ResponseEntity<CommonResponse<T>> badRequest(String message) {
        CommonResponse<T> commonResponse = new CommonResponse<>(HttpStatus.BAD_REQUEST, message);
        return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(commonResponse);
    }

    /**
     * 未授权：401
     *
     * @param message
     * @param <T>
     * @return
     */
    public static <T> ResponseEntity<CommonResponse<T>> unauthorized(String message) {
        CommonResponse<T> commonResponse = new CommonResponse<>(HttpStatus.UNAUTHORIZED, message);
        return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(commonResponse);
    }

    /**
     * 禁止访问：403
     *
     * @param message
     * @param <T>
     * @return
     */
    public static <T> ResponseEntity<CommonResponse<T>> forbidden(String message) {
        CommonResponse<T> commonResponse = new CommonResponse<>(HttpStatus.FORBIDDEN, message);
        return ResponseEntity.status(HttpStatus.FORBIDDEN).body(commonResponse);
    }

    /**
     * 未找到：404
     *
     * @param message
     * @param <T>
     * @return
     */
    public static <T> ResponseEntity<CommonResponse<T>> notFound(String message) {
        CommonResponse<T> commonResponse = new CommonResponse<>(HttpStatus.NOT_FOUND, message);
        return ResponseEntity.status(HttpStatus.NOT_FOUND).body(commonResponse);
    }

    /**
     * 内部服务器错误：500
     *
     * @param message
     * @param <T>
     * @return
     */
    public static <T> ResponseEntity<CommonResponse<T>> internalServerError(String message) {
        CommonResponse<T> commonResponse = new CommonResponse<>(HttpStatus.INTERNAL_SERVER_ERROR, message);
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(commonResponse);
    }

}
